package com.example.demo.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.demo.service.AccountService;
import com.example.demo.web.AjaxResult;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class AccountController {

    @Autowired
    private AccountService accountService;

    private static final String SUCCESS = "SUCCESS";

    private static final String FAIL = "FAIL";

    @PostMapping(value = "/account", produces = "application/json")
    public AjaxResult account(String userId, Integer money) {
        log.info("Account Service ... xid: " + RootContext.getXID());

        if (RandomUtil.randomBoolean()) {
            throw new RuntimeException("Account Service this is a mock Exception");
        }
        int result = accountService.account(userId, money);
        log.info("Account Service End ... ");
        if (result == 1) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
